﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using System.Collections;
using TdgMaker.DataType;
using System.IO;

namespace TdgMaker
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Rectangle rect = new Rectangle();
            rect = Screen.GetWorkingArea(this);
            this.SetDesktopBounds(0, 0, rect.Width, rect.Height - 30);
            this.Text = AboutBox1.AssemblyProduct + "   " + AboutBox1.AssemblyVersion;
            TB_DB.GetConn();
        }

        public int s_startLoc = 0;
        public int s_endLoc = 0;

        private void 应答器报文倒入ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Filter = "Balise Telegram Data (*.xls,*.XLS)|*.xls;*.XLS";
            fileDialog.FilterIndex = 1;
            fileDialog.RestoreDirectory = false;
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                ExlReader er = new ExlReader();
                er.xls_Name = fileDialog.FileName;
                er.sheet_Name = "Balise telegram$";
                er.ImportBaliseTelegram();
                MessageBox.Show("Balise Telegram Data Input Succeeded!");
            }
        }

        private void 轨道信息导入ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Filter = "Track Data (*.xls,*.XLS)|*.xls;*.XLS";
            fileDialog.FilterIndex = 1;
            fileDialog.RestoreDirectory = false;
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {

                ExlReader er = new ExlReader();
                er.xls_Name = fileDialog.FileName;
                er.sheet_Name = "up$";
                er.ImportTrackDataUpLine();
                er.sheet_Name = "down$";
                er.ImportTrackDataDownLine();


                string dirp = fileDialog.FileName.Replace(fileDialog.SafeFileName, "") + "\\Route\\";
                DirectoryInfo mydir = new DirectoryInfo(dirp);
                foreach (FileSystemInfo fsi in mydir.GetFileSystemInfos())
                {
                    if (fsi is FileInfo)
                    {
                        FileInfo fi = (FileInfo)fsi;
                        string x = System.IO.Path.GetExtension(fi.FullName);
                        if (x == ".xls")
                        {
                            Console.WriteLine(fi.FullName);
                            er.ImportRoute(fi.FullName);
                        }
                    }

                }
                MessageBox.Show("Track Data Input Succeeded!");
            }
        }

        private void 应答器位置导入ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Filter = "Balise Position Data (*.xls,*.XLS)|*.xls;*.XLS";
            fileDialog.FilterIndex = 1;
            fileDialog.RestoreDirectory = false;
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {

                ExlReader er = new ExlReader();
                er.xls_Name = fileDialog.FileName;
                er.sheet_Name = "up$";
                er.ImportBalisePos(0);

                er.sheet_Name = "down$";
                er.ImportBalisePos(1);

                MessageBox.Show("Balise Position Data Input Succeeded!");
            }
        }

        ArrayList AL = new ArrayList();
        ArrayList AL_stationTrack = new ArrayList();
        ArrayList TrackListDownLine = new ArrayList();
        ArrayList TrackListUpLine = new ArrayList();
        ArrayList AL_Route = new ArrayList();

        private void button1_Click(object sender, EventArgs e)
        {
            s_startLoc = Convert.ToInt32(textBox1.Text, 10);
            s_endLoc = Convert.ToInt32(textBox2.Text, 10);

            AL.Clear();
            AL_stationTrack.Clear();
            TrackListDownLine.Clear();
            TrackListUpLine.Clear();
            TrackListDownLine = TB_DB.GetTrackListByPos(1, s_startLoc, s_endLoc);
            TrackListUpLine = TB_DB.GetTrackListByPos(0, s_startLoc, s_endLoc);
            int listS = 0;
            ArrayList temp = new ArrayList();
            //AL.Add(temp);
            listS = 1;
            for (int i = 0; i < TrackListDownLine.Count; i++)
            {
                TrackData temptd = ((TrackData)TrackListDownLine[i]);
                if (temptd.SignalName == "X")
                {
                    listS = -1;
                    temp.Add(TrackListDownLine[i]);
                    ((TrackData)TrackListDownLine[i]).isInStation = true;
                }
                else if (temptd.SignalName == "SF")
                {
                    listS = 0;
                    if(i!=0)
                    {
                        temp = new ArrayList();
                        AL.Add(temp);
                        temp.Add(TrackListDownLine[i - 1]);
                        ((TrackData)TrackListDownLine[i - 1]).isInStation = true;
                    }
                    temp.Add(TrackListDownLine[i]);
                }
                else if (listS > 0)
                {
                    listS = 0;
                    temp = new ArrayList();
                    AL.Add(temp);
                    temp.Add(TrackListDownLine[i]);
                }
                else if (listS > -1)
                {
                    temp.Add(TrackListDownLine[i]);
                }
            }
            listS = 1;
            for (int i = 0; i < TrackListUpLine.Count; i++)
            {
                TrackData temptd = ((TrackData)TrackListUpLine[i]);
                if (temptd.SignalName == "S")
                {
                    listS = -1;
                    temp.Add(TrackListUpLine[i]);
                    ((TrackData)TrackListUpLine[i]).isInStation = true;
                }
                else if (temptd.SignalName == "XF")
                {
                    listS = 0;
                    if(i!=0)
                    {
                        temp = new ArrayList();
                        AL.Add(temp);
                        temp.Add(TrackListUpLine[i - 1]);
                        ((TrackData)TrackListUpLine[i - 1]).isInStation = true;
                    }
                    temp.Add(TrackListUpLine[i]);
                }
                else if (listS > 0)
                {
                    listS = 0;
                    temp = new ArrayList();
                    AL.Add(temp);
                    temp.Add(TrackListUpLine[i]);
                }
                else if (listS > -1)
                {
                    temp.Add(TrackListUpLine[i]);
                }
            }

            reListTrack();

            tabControl1.SelectedIndex = 1;
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (tabControl1.SelectedIndex)
            {
                case 0:
                    break;
                case 1:
                    break;
                case 2:
                    break;
                case 3:
                    break;
                case 4:
                    break;
            }
        }


        private void reListTrack()
        {
            checkedListBox1.Items.Clear();
            if (s_startLoc <= s_endLoc)
            {
                for (int i = 0; i < AL.Count; i++)
                {
                    try
                    {
                        checkedListBox1.Items.Add(((TrackData)((ArrayList)AL[i])[0]).TrackLoc.ToString() + "-->" + ((TrackData)((ArrayList)AL[i])[((ArrayList)AL[i]).Count - 1]).TrackLoc.ToString());
                    }
                    catch { }
                }
            }
            else
            {
                for (int i = 0; i < AL.Count; i++)
                {
                    try
                    {
                        checkedListBox1.Items.Add(((TrackData)((ArrayList)AL[i])[0]).TrackLoc.ToString() + "<--" + ((TrackData)((ArrayList)AL[i])[((ArrayList)AL[i]).Count - 1]).TrackLoc.ToString());
                    }
                    catch { }
                }
            }

        }

        private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            ArrayList temp = (ArrayList)AL[checkedListBox1.SelectedIndex];
            for (int i = 0; i < temp.Count; i++)
            {
                listBox1.Items.Add(((TrackData)temp[i]).getTrackName() + "\t" + ((TrackData)temp[i]).TrackLoc + "\t" + ((TrackData)temp[i]).Length + "\t" + ((TrackData)temp[i]).CF);
            }
            textBox10.Text = ((TrackData)temp[0]).getUpDown();
            textBox9.Text = ((TrackData)temp[0]).getReverse();
            textBox8.Text = temp.Count.ToString();
        }

        ArrayList AL_STATION = new ArrayList();
        private void reListStationTrack()
        {
            AL_STATION.Clear();
            ArrayList temp = new ArrayList();
            for (int i = 0; i < AL_stationTrack.Count; i++)
            {
                bool found = false;
                for (int j = 0; j < AL_STATION.Count; j++)
                {
                    if (((TrackData)((ArrayList)AL_STATION[j])[0]).getStationName() == ((TrackData)AL_stationTrack[i]).getStationName())
                    {
                        ((ArrayList)AL_STATION[j]).Add(AL_stationTrack[i]);
                        found = true;
                    }
                }

                if (!found)
                {
                    temp = new ArrayList();
                    temp.Add(AL_stationTrack[i]);
                    AL_STATION.Add(temp);
                }
            }
            listBox2.Items.Clear();
            for (int i = 0; i < AL_STATION.Count; i++)
                listBox2.Items.Add(((TrackData)((ArrayList)AL_STATION[i])[0]).getStationName());
        }

        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        ArrayList AL_RouteShow;
        private void button2_Click(object sender, EventArgs e)
        {
            AL_RouteShow = new ArrayList();
            for (int i = 0; i < AL.Count; i++)
            {
                if (checkedListBox1.GetItemChecked(i))
                {
                    for (int j = 0; j < ((ArrayList)AL[i]).Count; j++)
                        ((TrackData)((ArrayList)AL[i])[j]).selected = true;
                }
                else
                {
                    for (int j = 0; j < ((ArrayList)AL[i]).Count; j++)
                        ((TrackData)((ArrayList)AL[i])[j]).selected = false;
                }
            }
            AL_Route = TB_DB.GetRoute();
            checkedListBox2.Items.Clear();
            for (int i = 0; i < AL.Count; i++)
            {
                if (((TrackData)((ArrayList)AL[i])[0]).selected == true && s_startLoc <= s_endLoc)
                {
                    for (int j = 0; j < AL_Route.Count; j++)
                    {
                        if (((TrackData)((ArrayList)AL[i])[0]).SNameTName == ((RouteData)AL_Route[j]).includeTrack[((RouteData)AL_Route[j]).includeTrack.Length - 1].SNameTName)
                        {
                            ((RouteData)AL_Route[j]).bindTrack = ((TrackData)((ArrayList)AL[i])[0]);
                            //连接第一个区段，发车，向下行
                            ((RouteData)AL_Route[j]).UpDateBindLoc(false, true);
                            AL_RouteShow.Add(AL_Route[j]);
                            checkedListBox2.Items.Add(string.Format("{0}\t{1}\t\t{2}-->{3}\t\t{4}-->{5}", j, ((RouteData)AL_Route[j]).SName_RouteNr, ((RouteData)AL_Route[j]).includeTrack[0].getTrackName(), ((RouteData)AL_Route[j]).includeTrack[((RouteData)AL_Route[j]).includeTrack.Length - 1].getTrackName(), ((RouteData)AL_Route[j]).sSignal, ((RouteData)AL_Route[j]).eSignal));
                        }
                        if (((TrackData)((ArrayList)AL[i])[((ArrayList)AL[i]).Count - 1]).SNameTName == ((RouteData)AL_Route[j]).includeTrack[0].SNameTName)
                        {
                            ((RouteData)AL_Route[j]).bindTrack = ((TrackData)((ArrayList)AL[i])[((ArrayList)AL[i]).Count - 1]);
                            ((RouteData)AL_Route[j]).UpDateBindLoc(true, true);
                            //连接第一个区段，接车，向下行
                            AL_RouteShow.Add(AL_Route[j]);
                            checkedListBox2.Items.Add(string.Format("{0}\t{1}\t\t{2}-->{3}\t\t{4}-->{5}", j, ((RouteData)AL_Route[j]).SName_RouteNr, ((RouteData)AL_Route[j]).includeTrack[0].getTrackName(), ((RouteData)AL_Route[j]).includeTrack[((RouteData)AL_Route[j]).includeTrack.Length - 1].getTrackName(), ((RouteData)AL_Route[j]).sSignal, ((RouteData)AL_Route[j]).eSignal));
                        }
                    }
                }
                else if (((TrackData)((ArrayList)AL[i])[0]).selected == true && s_startLoc > s_endLoc)
                {
                    for (int j = 0; j < AL_Route.Count; j++)
                    {
                        if (((TrackData)((ArrayList)AL[i])[0]).SNameTName == ((RouteData)AL_Route[j]).includeTrack[0].SNameTName)
                        {
                            ((RouteData)AL_Route[j]).bindTrack = ((TrackData)((ArrayList)AL[i])[0]);
                            //连接第一个区段，接车，向上行
                            ((RouteData)AL_Route[j]).UpDateBindLoc(true, false);
                            AL_RouteShow.Add(AL_Route[j]);

                            checkedListBox2.Items.Add(string.Format("{0}\t{1}\t\t{2}-->{3}", j, ((RouteData)AL_Route[j]).SName_RouteNr, ((RouteData)AL_Route[j]).includeTrack[0].getTrackName(), ((RouteData)AL_Route[j]).includeTrack[((RouteData)AL_Route[j]).includeTrack.Length - 1].getTrackName()));
                        }
                        if (((TrackData)((ArrayList)AL[i])[((ArrayList)AL[i]).Count - 1]).SNameTName == ((RouteData)AL_Route[j]).includeTrack[((RouteData)AL_Route[j]).includeTrack.Length - 1].SNameTName)
                        {
                            ((RouteData)AL_Route[j]).bindTrack = ((TrackData)((ArrayList)AL[i])[((ArrayList)AL[i]).Count - 1]);
                            ((RouteData)AL_Route[j]).UpDateBindLoc(false, false);
                            //连接第一个区段，发车，向上行
                            AL_RouteShow.Add(AL_Route[j]);
                            checkedListBox2.Items.Add(string.Format("{0}\t{1}\t\t{2}-->{3}", j, ((RouteData)AL_Route[j]).SName_RouteNr, ((RouteData)AL_Route[j]).includeTrack[0].getTrackName(), ((RouteData)AL_Route[j]).includeTrack[((RouteData)AL_Route[j]).includeTrack.Length - 1].getTrackName()));
                        }
                    }
                }


            }
            tabControl1.SelectedIndex = 2;

        }


        private void button4_Click(object sender, EventArgs e)
        {
            richTextBox1.Text = "";

            richTextBox1.AppendText("Name: \"" + textBox_1.Text + "\"\n");
            richTextBox1.AppendText("DocNr: \"" + textBox_2.Text + "\"\n");
            richTextBox1.AppendText("Resp: \"" + textBox_3.Text + "\"\n");
            richTextBox1.AppendText("Date: \"" + textBox_4.Text + "\"\n");
            richTextBox1.AppendText("Rev: \"" + textBox_5.Text + "\"\n");
            richTextBox1.AppendText("\n");
            richTextBox1.AppendText("\n");


            richTextBox1.AppendText("TransponderIntervallDistance: " + textBox_6.Text + "\n");
            richTextBox1.AppendText("TransponderLength: " + textBox_7.Text + "\n");
            richTextBox1.AppendText("TypeOfLoco: \"" + textBox_8.Text + "\"\n");
            richTextBox1.AppendText("TypeOfPanel: \"" + textBox_9.Text + "\"\n");
            richTextBox1.AppendText("TypeOfTransponder: \"" + textBox_10.Text + "\"\n");
            richTextBox1.AppendText("\n");
            richTextBox1.AppendText("\n");



            for (int i = 0; i < BaliseList.Count; i++)
            {
                richTextBox1.AppendText(string.Format("//***************  {0}  {1}  {2}****************\n", ((BalisePos)BaliseList[i]).getBaliseNr(), ((BalisePos)BaliseList[i]).getNID_BG(), ((BalisePos)BaliseList[i]).Balise_Loc));
                richTextBox1.AppendText(string.Format("{0}\t IL INLINE830\t {1};\n", ((BalisePos)BaliseList[i]).myPos, ((BalisePos)BaliseList[i]).getTele()));
            }

            richTextBox1.AppendText("\n");
            richTextBox1.AppendText("\n");
            richTextBox1.AppendText("\n");
            richTextBox1.AppendText("\n");
            richTextBox1.AppendText("\n");
            richTextBox1.AppendText("\n");

            for (int i = 0; i < al_AllSelectedTrack.Count; i++)
            {
                richTextBox1.AppendText(string.Format("//***************  {0}:{1}:{2}   ****************\n", ((TrackData)al_AllSelectedTrack[i]).getTrackName(), ((TrackData)al_AllSelectedTrack[i]).Length, ((TrackData)al_AllSelectedTrack[i]).TrackLoc));
                richTextBox1.AppendText(string.Format("{0}\t TC INLINE16\t {1}12;\n", ((TrackData)al_AllSelectedTrack[i]).myPos, ((TrackData)al_AllSelectedTrack[i]).getCF()));
            }
            tabControl1.SelectedIndex = 4;
        }




        ArrayList al_AllSelectedTrack = new ArrayList();
        private void addAllSelected()
        {
            al_AllSelectedTrack.Clear();
            ///////////添加所有已经选择的区段，不包含站内
            for (int i = 0; i < TrackListDownLine.Count; i++)
            {
                if (((TrackData)TrackListDownLine[i]).selected && !((TrackData)TrackListDownLine[i]).isInStation)
                    al_AllSelectedTrack.Add((TrackData)TrackListDownLine[i]);
            }
            for (int i = 0; i < TrackListUpLine.Count; i++)
            {
                if (((TrackData)TrackListUpLine[i]).selected && !((TrackData)TrackListDownLine[i]).isInStation)
                    al_AllSelectedTrack.Add((TrackData)TrackListUpLine[i]);
            }
            ////////添加占内信息
            for (int i = 0; i < AL_RouteShow.Count; i++)
            {
                if (((RouteData)AL_RouteShow[i]).isSelected)
                {
                    for (int j = 0; j < ((RouteData)AL_RouteShow[i]).includeTrack.Length; j++)
                    {
                        al_AllSelectedTrack.Add(((RouteData)AL_RouteShow[i]).includeTrack[j]);
                    }
                }
            }


            myLocCompare mlc = new myLocCompare();
            al_AllSelectedTrack.Sort(mlc);
            if ((s_startLoc > s_endLoc))
            {
                al_AllSelectedTrack.Reverse();
            }
        }

        private void makeMyPos()
        {
            int myPos = 0;
            if (al_AllSelectedTrack.Count > 0 && s_startLoc <= s_endLoc)
            {
                if (al_AllSelectedTrack.Count>0&&((TrackData)al_AllSelectedTrack[0]).isDown)
                {
                    myPos = ((TrackData)al_AllSelectedTrack[0]).TrackLoc;
                }
                else
                {
                    myPos = ((TrackData)al_AllSelectedTrack[0]).TrackLoc - ((TrackData)al_AllSelectedTrack[0]).Length;
                }
                for (int i = 0; i < al_AllSelectedTrack.Count; i++)
                {
                    TrackData tempTD = ((TrackData)al_AllSelectedTrack[i]);
                    tempTD.myPos = myPos;
                    myPos += tempTD.Length;


                    for (int j = 0; j < BaliseList.Count; j++)
                    {
                        BalisePos tempBP = ((BalisePos)BaliseList[j]);
                        if (tempBP.SNameTName == tempTD.SNameTName)
                        {
                            if (tempTD.isDown)
                            {
                                tempBP.myPos = tempTD.myPos + tempBP.Track_Offset;
                            }
                            else
                            {
                                tempBP.myPos = tempTD.myPos + tempTD.Length - tempBP.Track_Offset;
                            }
                        }
                    }
                }

            }
            else if(al_AllSelectedTrack.Count>0)
            {
                if (al_AllSelectedTrack.Count>0&&(((TrackData)al_AllSelectedTrack[al_AllSelectedTrack.Count - 1]).isDown))
                {
                    myPos = ((TrackData)al_AllSelectedTrack[al_AllSelectedTrack.Count - 1]).TrackLoc + ((TrackData)al_AllSelectedTrack[al_AllSelectedTrack.Count - 1]).Length;
                }
                else
                {
                    myPos = ((TrackData)al_AllSelectedTrack[al_AllSelectedTrack.Count - 1]).TrackLoc;
                }
                for (int i = al_AllSelectedTrack.Count - 1; i > -1; i--)
                {
                    TrackData tempTD = ((TrackData)al_AllSelectedTrack[i]);
                    tempTD.myPos = myPos;
                    myPos -= tempTD.Length;


                    for (int j = 0; j < BaliseList.Count; j++)
                    {
                        BalisePos tempBP = ((BalisePos)BaliseList[j]);
                        if (tempBP.SNameTName == tempTD.SNameTName)
                        {
                            if (tempTD.isDown)
                            {
                                tempBP.myPos = tempTD.myPos + tempBP.Track_Offset - tempTD.Length;
                            }
                            else
                            {
                                tempBP.myPos = tempTD.myPos - tempBP.Track_Offset;
                            }
                        }
                    }
                }
            }

            for (int i = 0; i < AL_RouteShow.Count; i++)
            {
                if (((RouteData)AL_RouteShow[i]).isSelected)
                {
                    ((RouteData)AL_RouteShow[i]).analysisBalsie(BaliseList);
                    try
                    {
                        for (int j = 0; j < ((RouteData)AL_RouteShow[i]).includeBalise.Length; j++)
                        {
                            for (int m = 0; m < ((RouteData)AL_RouteShow[i]).includeBalise[j].baliseInGroup.Length; m++)
                            {
                                BaliseList.Add(((RouteData)AL_RouteShow[i]).includeBalise[j].baliseInGroup[m]);
                                ((RouteData)AL_RouteShow[i]).includeBalise[j].baliseInGroup[m].getTeleFromDB();
                                if (((RouteData)AL_RouteShow[i]).includeBalise[j].baliseInGroup[m].BaliseTele_List != null && ((RouteData)AL_RouteShow[i]).includeBalise[j].baliseInGroup[m].BaliseTele_List.Count > 1)
                                {
                                    listBox3.Items.Add((BaliseList.Count - 1).ToString().PadLeft(6, '0') + "\t" + ((RouteData)AL_RouteShow[i]).includeBalise[j].baliseInGroup[m].getBaliseNr() + "\t" + ((RouteData)AL_RouteShow[i]).includeBalise[m].baliseInGroup[m].Balise_Name);
                                }
                            }
                        }
                    }
                    catch { }
                }
            }

            BaliseLocCompare baliseLocCom = new BaliseLocCompare();
            BaliseList.Sort(baliseLocCom);

        }
        ArrayList BaliseList = new ArrayList();
        private void button3_Click(object sender, EventArgs e)
        {
            addAllSelected();
            getBaliseList();
            makeMyPos();
            tabControl1.SelectedIndex = 3;

        }

        private void getBaliseList()
        {
            BaliseList.Clear();
            for (int i = 0; i < al_AllSelectedTrack.Count; i++)
            {
                if (!((TrackData)al_AllSelectedTrack[i]).isInStation)
                TB_DB.GetBaliseList(((TrackData)al_AllSelectedTrack[i]).SNameTName, BaliseList);
            }
            for (int i = 0; i < BaliseList.Count; i++)
            {
                ((BalisePos)BaliseList[i]).BaliseTele_List = TB_DB.GetBaliseTele(((BalisePos)BaliseList[i]).Balise_Nr);
                if (((BalisePos)BaliseList[i]).BaliseTele_List != null && ((BalisePos)BaliseList[i]).BaliseTele_List.Count > 1)
                {
                    listBox3.Items.Add(i.ToString().PadLeft(6, '0') + "\t" + ((BalisePos)BaliseList[i]).getBaliseNr() + "\t" + ((BalisePos)BaliseList[i]).Balise_Name);
                }
            }

        }

        private void listBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox1.Items.Clear();
            string[] s = listBox3.SelectedItem.ToString().Split('\t');
            int n = Convert.ToInt32(s[0], 10);
            BalisePos pb = ((BalisePos)BaliseList[n]);
            for (int i = 0; i < pb.BaliseTele_List.Count; i++)
                comboBox1.Items.Add(((BaliseTele)pb.BaliseTele_List[i]).Tele_Version + "\t" + ((BaliseTele)pb.BaliseTele_List[i]).Tele);
            comboBox1.SelectedIndex = pb.selectedTele_Index;

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string[] s = listBox3.SelectedItem.ToString().Split('\t');
            int n = Convert.ToInt32(s[0], 10);
            BalisePos pb = ((BalisePos)BaliseList[n]);
            pb.selectedTele_Index = comboBox1.SelectedIndex;

            textBox3.Text = pb.getStationName();
            textBox4.Text = pb.Balise_Name;
            textBox5.Text = ((BaliseTele)pb.BaliseTele_List[pb.selectedTele_Index]).Tele_Version;
            textBox6.Text = pb.getTrackName();

        }

        private void SaveTo_ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                WriteTdg.file = saveFileDialog1.FileName;
                WriteTdg.WriteIt(richTextBox1.Text);
            }

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

            ArrayList temp = (ArrayList)AL[checkedListBox1.SelectedIndex];
            TrackData td = (TrackData)temp[listBox1.SelectedIndex];
            textBox_TI1.Text = td.getStationName();
            textBox_TI2.Text = td.getTrackName();
            textBox_TI3.Text = td.Length.ToString();
            textBox_TI4.Text = td.CF;
            textBox_TI5.Text = td.TrackLoc.ToString();
            textBox_TI6.Text = td.Type;
        }

        private void Exit_ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void checkedListBox2_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            checkedListBox2.SelectedIndex = e.Index;
            RouteData temp = (RouteData)(AL_Route[Convert.ToInt32(checkedListBox2.SelectedItem.ToString().Split('\t')[0], 10)]);
            //ArrayList temp = (ArrayList)AL_STATION[listBox2.SelectedIndex];

            if (e.NewValue == CheckState.Checked)
            {
                temp.isSelected = true;
            }
            else
            {
                temp.isSelected = false;
            }


        }

        private void xClearDBToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TB_DB.clearAll();
        }

        private void checkedListBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            //显示进路里包含区段信息
            listBox2.Items.Clear();
            RouteData temp = (RouteData)(AL_Route[Convert.ToInt32(checkedListBox2.SelectedItem.ToString().Split('\t')[0], 10)]);
            for (int i = 0; i < temp.includeTrack.Length; i++)
            {
                listBox2.Items.Add(string.Format("{0}\t{1}\t{2}", temp.includeTrack[i].getTrackName(), temp.includeTrack[i].Length, temp.includeTrack[i].getCF()));
            }
            textBox_r1.Text = temp.SName_RouteNr;
            textBox_r2.Text = temp.BaliseNr;
            textBox_r3.Text = temp.IL_Nr;
            textBox_r4.Text = temp.sSignal+"==>"+temp.eSignal;
            textBox_r5.Text = temp.PassBalise;
            textBox_r6.Text = temp.PassSwith;
            textBox_r7.Text = temp.PassTracks;
            textBox_r8.Text = temp.RouteType;
            textBox_r9.Text = temp.Speeds;

            //显示进路里包含应答器信息
        }

        private void About_ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox1 ab = new AboutBox1();
            ab.ShowDialog();
        }

       

    public static    Settings1 s1 = new Settings1();

        private void showToolStripMenuItem_Click(object sender, EventArgs e)
        {
            tabControl2.Visible = true;
            
            tt1.Text = s1.TrackSet1;
            tt2.Text = s1.TrackSet2;
            tt3.Text = s1.TrackSet3;
            tt4.Text = s1.TrackSet4;
            tt5.Text = s1.TrackSet5;
            tt6.Text = s1.TrackSet6;
            tt7.Text = s1.TrackSet7;
            tt8.Text = s1.TrackSet8;
            tt9.Text = s1.TrackSet9;
            tt10.Text = s1.TrackSet10;

            tr1.Text = s1.Route1;
            tr2.Text = s1.Route2;
            tr3.Text = s1.Route3;
            tr4.Text = s1.Route4;
            tr5.Text = s1.Route5;
            tr6.Text = s1.Route6;
            tr7.Text = s1.Route7;
            tr8.Text = s1.Route8;
            tr9.Text = s1.Route9;
            tr10.Text = s1.Route10;

            tb1.Text = s1.BalisePos1;
            tb2.Text = s1.BalisePos2;
            tb3.Text = s1.BalisePos3;
            tb4.Text = s1.BalisePos4;
            tb5.Text = s1.BalisePos5;
            tb6.Text = s1.BalisePos6;
            tb7.Text = s1.BalisePos7;
            tb8.Text = s1.BalisePos8;
        }

        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            tabControl2.Visible = false;
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

      


    }


    class myLocCompare : System.Collections.IComparer
    {
        public int Compare(object x, object y)
        {
            return ((TrackData)x).TrackLoc - ((TrackData)y).TrackLoc;
        }
    }
    class BaliseLocCompare : System.Collections.IComparer
    {
        public int Compare(object x, object y)
        {
            return ((BalisePos)x).myPos - ((BalisePos)y).myPos;
        }
    }
}
